<template>
  <div class="students-management">
    <h2 style="margin-bottom: 20px">学生管理</h2>

    <!-- 搜索表单 -->
    <div class="search-form">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="用户ID">
          <el-input
            v-model="searchForm.userId"
            placeholder="请输入用户ID"
          ></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input
            v-model="searchForm.nickname"
            placeholder="请输入昵称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 学生信息表格 -->
    <div class="table-container">
      <el-table :data="students" style="width: 100%; margin: 0" border fit>
        <el-table-column
          prop="userId"
          label="用户ID"
        ></el-table-column>
        <el-table-column
          prop="nickname"
          label="昵称"
        ></el-table-column>
        <el-table-column prop="role" label="身份"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              type="danger"
              size="small"
              @click="deleteStudent(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  name: "StudentsManagement",
  data() {
    return {
      // 搜索表单数据
      searchForm: {
        userId: "",
        nickname: "",
      },
      // 学生数据
      students: [
        { userId: "123", nickname: "小掌", role: "学生" },
        { userId: "124", nickname: "小李", role: "学生" },
        { userId: "125", nickname: "小王", role: "学生" },
        { userId: "126", nickname: "小张", role: "学生" },
        { userId: "127", nickname: "小赵", role: "学生" },
        { userId: "128", nickname: "小钱", role: "学生" },
        { userId: "129", nickname: "小孙", role: "学生" },
      ],
    };
  },
  methods: {
    // 搜索学生
    onSearch() {
      const { userId, nickname } = this.searchForm;
      this.students = this.students.filter(
        (student) =>
          student.userId.includes(userId) && student.nickname.includes(nickname)
      );
    },
    // 删除学生
    deleteStudent(student) {
      this.students = this.students.filter((s) => s.userId !== student.userId);
      this.$message.success("删除成功");
    },
  },
};
</script>

<style scoped>
.students-management {
  padding: 20px;
  width: 100%;
}

/* .search-form {
    margin-bottom: 20px;
  } */

.table-container {
  width: 100%; /* 调整表格容器的宽度 */
  max-width: 1200px; /* 设置最大宽度 */
  margin: 0 auto; /* 居中 */
}

.demo-form-inline {
  display: flex;
  align-items: center;
}

.el-form-item {
  margin-right: 10px;
}

.el-table {
  margin-top: 20px;
}
</style>
